<script setup>

import {handleCurrentChange} from "element-plus/es/components/tree/src/model/util";
import {reactive} from "vue";
import {add} from "core-js/internals/set-helpers";
import xx from "@/axios/xx";
import {ElMessage} from "element-plus";


const info=reactive({
  list:[],  //列表数据
  form:{},  //表单数据
  dialogVisible:false, //对话框数据
  pageNum:1,
  pageSize:5,
  total:0,
  isAdd:true,  //增加和修改标识
})

//查询列表信息
const load = () => {
  xx.get('/bankInfo',{
    params:{
      pageNum:info.pageNum,
      pageSize:info.pageSize,
      bankName:info.form.bankName  //传递参数查询
    }
  }).then(res => {
    console.log(res)
    info.list=res.data.list
    info.pageNum=res.data.pageNum  //当前页
    info.pageSize=res.data.pageSize  //每页大小
    info.total=res.data.total  //总条数
  })
}
load()

//分页当前页改变
const handleCurrentChange1 = (pageNum) => {
  info.pageNum=pageNum
  load()  //重新检索数据
}

//每页大小改变
const handleSizeChange1 = (pageSize) => {
  info.pageSize=pageSize
  load()
}

//增加按钮点击方法
const add1 = () => {
  info.dialogVisible=true
  info.isAdd=true
}
//持久化数据
const persistence = () => {
  xx({
    url:'/bankInfo',
    method:info.isAdd?'post':'put',
    data:info.form
  }).then(res=>{
    if(res.code === 200){
      ElMessage({
        message: res.message,
        type: 'success',
      })
    }else{
      ElMessage.error(res.message)
    }
    //关闭对话框
    info.dialogVisible=false
    info.form={}  //清空数据
    load()
  })
}

//关闭对话框，清除数据
const handleClose = () => {
  info.form={}
  info.dialogVisible=false
}

//点击修改按钮-根据id查询对应数据
const handleEdit = (bankId) => {
  info.isAdd=false
  xx.get('bankInfo/'+bankId).then(res=>{
    info.form=res.data
    info.dialogVisible=true
  })

}
//删除
const handleDelete = (bankId) => {
  xx.delete('/bankInfo/'+bankId).then(res=>{
    console.log(res)
    if(res.code === 200){
      ElMessage({
        message: res.message,
        type: 'success',
      })
    }else{
      ElMessage.error(res.message)
    }
    load()
  })
}


</script>

<template>
  <!--增加按钮-->
  <el-button type="success" @click="add1">增加银行信息</el-button>
  <br><br>

  <!--快捷查询-->
  <el-form :inline="true" :model="info.form" class="demo-form-inline">
    <el-form-item label="类型名称">
      <el-input v-model="info.form.bankName" placeholder="模糊查询" clearable />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="load">快捷查询</el-button>
    </el-form-item>
  </el-form>


  <!--表格信息-->
  <el-table :data="info.list"  stripe border style="width: 100%">
    <el-table-column type="index" label="序号" width="50" align="center"/>
    <el-table-column prop="bankName" label="银行名称" width="180" align="center"/>
    <el-table-column prop="bankAddress" label="银行地址" width="230" align="center"/>
    <el-table-column prop="phoneNumber" label="联系电话" width="180" align="center"/>
    <el-table-column prop="email" label="电子邮件" width="200" align="center"/>
    <el-table-column prop="website" label="官方网站" width="180" align="center"/>
    <el-table-column prop="establishedYear" label="成立年份" width="80" align="center"/>
    <el-table-column label="操作"  align="center">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row.bankId)">修改</el-button>
        <el-popconfirm @confirm="handleDelete(scope.row.bankId)" title="确定要删除吗?">
          <template #reference>
            <el-button size="small" type="danger">删除</el-button>
          </template>
        </el-popconfirm>

      </template>
    </el-table-column>
  </el-table>

  <!--点击增加按钮出现的窗口-->
  <el-dialog
      v-model="info.dialogVisible"
      :title="info.isAdd ? '增加银行信息':'修改银行信息' "
      width="500"
      :before-close="handleClose"
  >
    <el-form :model="info.form" label-width="auto" style="max-width: 90%;margin-left: 20px">
      <el-form-item label="银行名称">
        <el-input v-model="info.form.bankName" />
      </el-form-item>
      <el-form-item label="银行地址">
        <el-input v-model="info.form.bankAddress" type="textarea" />
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="info.form.phoneNumber" type="textarea" />
      </el-form-item>
      <el-form-item label="电子邮件">
        <el-input v-model="info.form.email" type="textarea" />
      </el-form-item>
      <el-form-item label="官网地址">
        <el-input v-model="info.form.website" type="textarea" />
      </el-form-item>
      <el-form-item label="成立年份">
        <el-input v-model="info.form.establishedYear" type="textarea" />
      </el-form-item>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="persistence">
          保存信息
        </el-button>
      </div>
    </template>
  </el-dialog>


  <!--分页-->
  <br><br>
  <el-pagination
      v-model:current-page="info.pageNum"
      v-model:page-size="info.pageSize"
      :page-sizes="[5,10,15,20]"
      :small="false"
      :disabled="false"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="info.total"
      @size-change="handleSizeChange1"
      @current-change="handleCurrentChange1"
  />

</template>

<style scoped>

</style>